import 'package:flutter/material.dart';
import 'package:flutter_rfid_dmo/rfid/rfid_connection.dart'
    show RFIDConnection;
import 'barcode_page.dart';
import 'hight_rfid_page.dart';
import 'rfid_page.dart'; // 引入 RFIDPage
import 'ecp_page.dart'; // 引入 EPCPage

class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  MainPageState createState() => MainPageState();
}

class MainPageState extends State<MainPage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  _handlePageChange(int newPage) {
    if (_currentPage != newPage) {
      int? on;
      if (newPage == 1) {
        on = 1;
      } else if (newPage == 2) {
        on = 0;
      } else if (newPage == 3) {
        on = 2;
      }
      if (on != null) {
        RFIDConnection().setPower(on);
      }
    }
    _currentPage = newPage;
  }

  int _currentPage = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('RFID Demo'),
      ),
      body: PageView(
        reverse: false, // 不反转
        physics: const AlwaysScrollableScrollPhysics(), // 禁用手势滑动
        pageSnapping: true, // 启用页面捕捉
        onPageChanged: _handlePageChange,
        children: const [
          RFIDPage(), // RFID页面
          EPCPage(), // EPC页面
          BarcodePage(), // 条码页面
          HightRfidPage(), // 高频RFID寻卡
        ],
      ),
    );
  }
}
